<?

/**
*------------------------------------------------------------------------------
* if approved
*
*/
if($isApprove == true){
    
  /**
  * We extract all neccesary info
  * of this jobId ($apprThisJobId)
  *
  */
  fetchTxDetail($COracle,
                $apprThisJobId,
                $idTx,
                $srcDbDtl,
                $resDetail);
  
  /**
  * We update the received Qty
  *
  */
  $nRows = sizeof($resDetail["NO"]);
  for($iRow = 0; $iRow < $nRows; $iRow++){
    
    /**
    * We check the target's received Qty
    * and add with current received qty 
    *
    */
    fetchOldReceived($COracle,
                     $resDetail["ORDER_REF"][$iRow],
                     $resDetail["ORDER_NO"][$iRow],
                     $dstDbSubDtl,
                     $resOldDlvr);
    dbgInfo("resOldDlvr", $resOldDlvr); dbgInfo("thisDlvr", $resDetail["DLVR_QTY"][$iRow]);
    $currDlvrQty = $resOldDlvr + $resDetail["DLVR_QTY"][$iRow];
    dbgInfo("currDlvrQty",$currDlvrQty);
    updateDlvrQty($COracle,
                  $resDetail["ORDER_REF"][$iRow],
                  $resDetail["ORDER_NO"][$iRow],
                  $currDlvrQty,
                  $dstDbSubDtl);
  }
  
  /**
  * for each detail check if one 
  * of the tx is
  * Equal update the status to
  * YES: _BTB_ISSUED
  * NO : _OUTSTANDING
  *
  */
  $isOutstanding = false;
  $isBtbIssued   = false;
  $isActive      = false;
  
  for($iRow = 0; $iRow < $nRows; $iRow++){
  
    chkDlvrQuantity($COracle,
                    $resDetail["ORDER_REF"][$iRow],
                    $dstDbSubDtl,
                    $isOutstanding,
                    $isBtbIssued,
                    $isActive);
    
    updateStatus($COracle,
                 $resDetail["ORDER_REF"][$iRow],
                 $isOutstanding,
                 $isBtbIssued,
                 $isActive,
                 $dstDbHdr);
                 
    $isOutstanding = false;
    $isBtbIssued   = false;
    $isActive      = false;
  }
}else{

  /**
  *------------------------------------------------------------------------------
  * if canceled
  *
  */
  
  /**
  * We extract all neccesary info
  * of this jobId ($cclThisJobId)
  *
  */
  fetchTxDetail($COracle,
                $cclThisJobId,
                $idTx,
                $srcDbDtl,
                $resDetail);
                
  /**
  * Restore back the remainder
  *
  */
  $nRows = sizeof($resDetail["NO"]);
  for($iRow = 0; $iRow < $nRows; $iRow++){
    
    fetchOldReceived($COracle,
                     $resDetail["ORDER_REF"][$iRow],
                     $resDetail["ORDER_NO"][$iRow],
                     $dstDbSubDtl,
                     $resOldDlvr);
    
    $currDlvrQty = $resOldDlvr - $resDetail["DLVR_QTY"][$iRow];
    
    updateDlvrQty($COracle,
                  $resDetail["ORDER_REF"][$iRow],
                  $resDetail["ORDER_NO"][$iRow],
                  $currDlvrQty,
                  $dstDbSubDtl);
  }
  
  $isBtbIssued    = false;
  $isOutstanding  = false;
  $isActive       = false;
  
  for($iRow = 0; $iRow < $nRows; $iRow++){
  
    chkDlvrQuantity($COracle,
                    $resDetail["ORDER_REF"][$iRow],
                    $dstDbSubDtl,
                    $isOutstanding,
                    $isBtbIssued,
                    $isActive);
    
    updateStatus($COracle,
                 $resDetail["ORDER_REF"][$iRow],
                 $isOutstanding,
                 $isBtbIssued,
                 $isActive,
                 $dstDbHdr);
                                 
    $isBtbIssued    = false;
    $isOutstanding  = false;
    $isActive       = false;
  }
}
?>